#!/bin/bash
#
# PB138 Task 3 Checker
# created by Tomáš Pitner, April 2011
# version 1.0, jaro 2011
#
# * Put this script into the same directory where the ZIP
#   with all solutions reside.
# * Put also the check-all-task3 here.
# * Make them both executable.
#
# * Run either this for single solution checking:
#   ./check-task3 Some_Student_UCO_blabla.zip
#
#   or to check all zipped in one file:
#   ./check-all-task3 uloha3.zip
#
# * The result of the XSLT transformation 
#   is stored to the same dir as the orig ZIP file resides
#   in the file <name>-address-book.html extension.
#   Errors encountered during the transformation are
#   in the file <name>-result.txt extension.
#
# * Overall results are present in:
#   
#   ok.txt             -- names of ZIPs that were transformer OK 
#                         (HTML5/XHTML validity is NOT checked with this!!!)
#   bad-zips.txt       -- names of badly zipped ZIP archives
#   missing-files.txt  -- Nonexistent address-book-to-html.xsl
#
# * System requirements: a Linux with xsltproc, zip/unzip, bash
#

ZIP_FILE=$1
unzip $ZIP_FILE

# ensure there is no MacOS X stuff...
if [ -d "__MACOSX" ]; then
rm -rf "__MACOSX"
fi

# what is the created dir?
TOP="$(ls --group-directories-first|head -n 1)"
DIR="$TOP/src"

# if exists, change to it
if [ -d "$DIR" ]; then
cd "$DIR"
echo "Processing $PWD..."

# does the required style exist?
if [ -f address-book-to-html.xsl ]; then

# yes, then process it 
xsltproc -o address-book.html address-book-to-html.xsl address-book.xml >> result.txt

# save the result and generated HTML
cat result.txt | tee ../../$1-result.txt
cp address-book.html ../../$1-address-book.html
cd ../..

# zip it all
zip -u -r -q $ZIP_FILE "$TOP"/

echo "Done OK $ZIP_FILE" >> ok.txt

else 
	echo "Nonexistent address-book-to-html.xsl"
	echo $ZIP_FILE >> ../../missing-files.txt
fi
else
	echo "Badly zipped solution, $DIR does not exist"
	echo $ZIP_FILE >> bad-zips.txt
fi
rm -rf "$TOP"
